<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/bean/Abstract.php");
require_once("cms/core/om/CategoryPeer.php");
require_once("cms/core/om/CategoryI18nPeer.php");

abstract class AbstractCategories extends AbstractList {

    public function init($form) {

        if($this->isInit)
            return $this;

        parent::init($form);

        $peer = $this->getCountPeer();

        $this->criteria->addAsColumn('count', 'COUNT('.$this->getCountPrimaryKey().')');

        $this->criteria->addGroupByColumn("category_i18n.category_id");

        $this->criteria->addJoin(CategoryI18nPeer::CATEGORY_ID,
            $this->getCountForeignKey(), Criteria::LEFT_JOIN);

        $this->criteria->addJoin(CategoryI18nPeer::CATEGORY_ID,
            CategoryPeer::CATEGORY_ID, Criteria::LEFT_JOIN);

        if(strlen($form->getLanguage()) > 0)
            $this->criteria->add(CategoryI18nPeer::LANGUAGE_CODE,
                $form->getLanguage());

        if(strlen($form->getQuery()) == 0 && strlen($form->getLanguage()) == 0) {

            $this->criteria->add(
                CategoryI18nPeer::LANGUAGE_CODE,
                $this->param(_Core::DEFAULT_LANGUAGE)
            );

        }

        $this->criteria->add(CategoryPeer::MODULE, $this->getModule());

        $this->criteria->setDistinct();

        return $this;
    }


    abstract public function getCountPrimaryKey();

    abstract public function getCountForeignKey();

    abstract public function getCountPeer();

    abstract public function getModule();


    public function getCategories($categoryId, $name, $id) {

        $c = new Criteria();

        $c->clearSelectColumns();
        $c->addSelectColumn(CategoryI18nPeer::PAGE_CATEGORY_ID);
        $c->addSelectColumn(CategoryI18nPeer::NAME);

        $c->add(CategoryI18nPeer::LANGUAGE_CODE, $this->param(_Core::DEFAULT_LANGUAGE));
        $c->addAscendingOrderByColumn(CategoryI18nPeer::NAME);

        $rs = CategoryI18nPeer::doSelectRS($c);

        ?>
        <select name="<?=$name?>" id="<?=$id?>" class="textInput">
            <option value="">--</option>
            <? while($rs->next()): ?>
                <option value="<?=$rs->getString(1)?>"<?=$categoryId == $rs->getInt(1) ? " selected=\"selected\"" : ""?>><?=$rs->getString(2)?></option>
            <? endwhile; ?>
        </select>
        <?
    }


    /**
     * Define the propel peer we are going to use to list
     * categorys.
     */
    public static function getPeer() {
        return new CategoryI18nPeer();
    }


    /**
     * Set the searchable fields for this list page.
     */
    public static function getFields() {
        return array(
            CategoryI18nPeer::NAME,
            CategoryI18nPeer::DESCRIPTION,
            CategoryI18nPeer::INSERT_DATE,
            CategoryI18nPeer::MODIFIED_DATE
        );
    }
}
?>